package onion.molingo2o.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import onion.molingo2o.R;
import onion.molingo2o.activity.request.MyRequeSettingsActivity;
import onion.molingo2o.activity.request.NewRequeSendActivity;
import onion.molingo2o.activity.request.Reque2ResponListActivity;
import onion.molingo2o.activity.request.SearchActivity;
import onion.molingo2o.adapter.OnionAdapter;
import onion.molingo2o.adapter.OnionViewPageAdapter;
import onion.molingo2o.bean.RequestMoling;
import onion.molingo2o.bean.RequestTopMenu;
import onion.molingo2o.utils.StrUtils;
import onion.molingo2o.utils.UIUtils;
import onion.molingo2o.views.swipemenulistview.SwipeMenu;
import onion.molingo2o.views.swipemenulistview.SwipeMenuCreator;
import onion.molingo2o.views.swipemenulistview.SwipeMenuItem;
import onion.molingo2o.views.swipemenulistview.SwipeMenuListView;

/**
 * Created by Administrator on 2015/11/10.
 */
public class RequestsFragment extends BaseFragment {
    private OnionAdapter adapter, gvAdapter, gvAdapterMore;
    private List<RequestMoling> list = new ArrayList<>();
    private List<RequestTopMenu> topMenus = new ArrayList<>();
    private SwipeMenuListView listView;
    private GridView gridview_request, gridview_request_more;
    private ImageView iv_request_setting;

    private ViewPager viewPager;
    private OnionViewPageAdapter onionViewPageAdapter;
    /*索引*/
    private ImageView[] mIndicators;
    private LinearLayout index_indicator;
    //    private InputMethodManager inputMethodManager;
    TextView query;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_request_list, container, false);
    }

    int bottomVisibleIndex;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        listView = (SwipeMenuListView) getView().findViewById(R.id.list);
        initHeadView();
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                // Create different menus depending on the view type
                switch (menu.getViewType()) {
                    case 0:
                        createMenu1(menu);
                        break;
//                    case 1:
//                        createMenu2(menu);
//                        break;
//                    case 2:
//                        createMenu3(menu);
//                        break;
                }
            }
        };
        listView.setMenuCreator(creator);
        iv_request_setting = (ImageView) getView().findViewById(R.id.iv_request_setting);
        iv_request_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyRequeSettingsActivity.class));
            }
        });
        //搜索框
        query = (TextView) getView().findViewById(R.id.query);
        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });
        // 设置adapter
        adapter = new OnionAdapter(getActivity(), R.layout.item_requests, list, new String[]{"title", "time", "content", "num"}, new String[]{"getIcon"});
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), Reque2ResponListActivity.class);
                startActivity(intent);
            }
        });
        // 获取设置contactlist
        getTest();
    }

    private void initHeadView() {
        viewPager = (ViewPager) getView().findViewById(R.id.viewpager);
        index_indicator = (LinearLayout) getView().findViewById(R.id.index_indicator);
        viewPager.setOnPageChangeListener(new ViewPageChangeListener());
    }

    private void createMenu1(SwipeMenu menu) {
        SwipeMenuItem item1 = new SwipeMenuItem(
                getActivity().getApplicationContext());
        item1.setBackground(new ColorDrawable(Color.rgb(0xE5, 0x18,
                0x5E)));
        item1.setWidth(UIUtils.dp2px(90));
        item1.setTitle("终止");
        item1.setTitleColor(Color.WHITE);
        item1.setTitleSize(18);
//        item1.setIcon(R.drawable.ic_action_favorite);
        menu.addMenuItem(item1);
        SwipeMenuItem item2 = new SwipeMenuItem(
                getActivity().getApplicationContext());
        item2.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                0xCE)));
        item2.setWidth(UIUtils.dp2px(90));
        item2.setTitleColor(Color.WHITE);
        item2.setTitleSize(18);
        item2.setTitle("作废");
//        item2.setIcon(R.drawable.ic_action_good);
        menu.addMenuItem(item2);
    }

    private void getTest() {
        for (int i = 0; i < 10; i++) {
            list.add(new RequestMoling("http://img0.imgtn.bdimg.com/it/u=1237455343,3383829147&fm=21&gp=0.jpg", "找小鸡", "02:46", "胸大腿长腰细", "33"));
        }
        for (int i = 0; i < 16; i++) {
            topMenus.add(new RequestTopMenu("http://g.hiphotos.baidu.com/image/h%3D200/sign=16f4ef3e35adcbef1e3479069cae2e0e/6d81800a19d8bc3e7763d030868ba61ea9d345e5.jpg", "挖坑"));
        }
        initTopMenus();
        adapter.notifyDataSetChanged();
    }

    private void initTopMenus() {
        /*初始化索引*/
        mIndicators = new ImageView[topMenus.size() / 8];
        int num = UIUtils.dp2px(8);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(num, num);
        params.setMargins(10, 0, 10, 0);
        for (int i = 0; i < mIndicators.length; i++) {
            mIndicators[i] = new ImageView(getActivity());
            mIndicators[i].setLayoutParams(params);
            index_indicator.addView(mIndicators[i]);
        }
        ArrayList<View> views = new ArrayList<>();
        gridview_request = new GridView(getActivity());
        gridview_request.setNumColumns(4);
        List<RequestTopMenu> menuList1 = new ArrayList<>();
        for (int i = 0; i < topMenus.size() && i < 8; i++) {
            menuList1.add(topMenus.get(i));
        }
        gvAdapter = new OnionAdapter(getActivity(), R.layout.item_smal_icon, menuList1, new String[]{"title"}, new String[]{"getIcon"});
        gridview_request.setAdapter(gvAdapter);
        gridview_request.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), NewRequeSendActivity.class);
                startActivity(intent);
            }
        });
        views.add(gridview_request);
        if (topMenus.size() > 8) {
            gridview_request_more = new GridView(getActivity());
            gridview_request_more.setNumColumns(4);
            List<RequestTopMenu> menuList2 = new ArrayList<>();
            for (int i = 8; i < topMenus.size() && i < 16; i++) {
                menuList2.add(topMenus.get(i));
            }
            gvAdapterMore = new OnionAdapter(getActivity(), R.layout.item_smal_icon, menuList2, new String[]{"title"}, new String[]{"getIcon"});
            gridview_request_more.setAdapter(gvAdapterMore);
            gridview_request_more.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getActivity(), NewRequeSendActivity.class);
                    startActivity(intent);
                }
            });
            views.add(gridview_request_more);
        }
        onionViewPageAdapter = new OnionViewPageAdapter(views);
        viewPager.setAdapter(onionViewPageAdapter);
        setImageBackground(0);
    }

    /**
     * 当ViewPager中页面的状态发生改变时调用
     *
     * @author Administrator
     */
    private class ViewPageChangeListener implements ViewPager.OnPageChangeListener {

        /**
         * This method will be invoked when a new page becomes selected.
         * position: Position index of the new selected page.
         */
        public void onPageSelected(int position) {
            setImageBackground(position);
        }

        public void onPageScrollStateChanged(int arg0) {

        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }
    }

    /**
     * 设置选中的tip的背景
     *
     * @param selectItemsIndex
     */
    private void setImageBackground(int selectItemsIndex) {
        for (int i = 0; i < mIndicators.length; i++) {
            if (i == selectItemsIndex) {
                mIndicators[i]
                        .setImageResource(R.drawable.red_point);
            } else {
                mIndicators[i]
                        .setImageResource(R.drawable.red_point_alpha);
            }
        }
    }

}
